package com.android.datasave;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.TrafficStats;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class DataService extends Service {
	
	private NotificationManager mNM;

    private int NOTIFICATION = R.string.local_service_started;

    public class LocalBinder extends Binder {
        DataService getService() {
            return DataService.this;
        }
    }

	@Override	
	public void onCreate() {
		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	}
	
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
		
        Intent myIntent = new Intent(DataService.this, DataService.class);
        PendingIntent pendingIntent = PendingIntent.getService(
                        DataService.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, Config.intervalleRafraichissement);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        pendingIntent);

        
        long mobile = TrafficStats.getMobileRxBytes() / 1024;
		//long total = TrafficStats.getTotalRxBytes() / 1024;
		//long wifi = total - mobile;
		LibFile.WriteFile(getApplicationContext(), String.valueOf(mobile));
        return START_STICKY;
    }

	@Override
    public void onDestroy() {
        mNM.cancel(NOTIFICATION);
        Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }

	@Override
	public IBinder onBind(Intent intent) {
		Toast.makeText(this, "DataService.onBind()", Toast.LENGTH_LONG).show();
		return null;
	}
	
	private final IBinder mBinder = new LocalBinder();

	@Override
	public boolean onUnbind(Intent intent) {
		Toast.makeText(this, "DataService.onUnbind()", Toast.LENGTH_LONG).show();
		return super.onUnbind(intent);
	}
}
